﻿using Laharsub.Server.Wcf;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Collections.Specialized;
using System.Text;

namespace Laharsub.Server.Wcf.Test
{
    
    
    /// <summary>
    ///This is a test class for PubSubServiceTest and is intended
    ///to contain all PubSubServiceTest Unit Tests
    ///</summary>
    [TestClass()]
    public class PubSubServiceTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for PubSubService Constructor
        ///</summary>
        [TestMethod()]
        public void PubSubServiceConstructorTest()
        {
            PubSubService target = new PubSubService();
        }

        /// <summary>
        ///A test for CreateMultipartStream: two messages
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Laharsub.exe")]
        public void CreateMultipartStreamTest()
        {
            PubSubService_Accessor target = new PubSubService_Accessor(); // TODO: Initialize to an appropriate value
            string boundary = "--abcdefg";
            PubSubMessage[] messages = new PubSubMessage[] {
                new PubSubMessage { MessageId = 100, TopicId = 200, ContentType = "text/plain", Body = Encoding.UTF8.GetBytes("Hello1") },
                new PubSubMessage { MessageId = 300, TopicId = 400, ContentType = "text/html", Body = Encoding.UTF8.GetBytes("Hello2") },
            };
            Stream actual = target.CreateMultipartStream(boundary, messages);
            Assert.IsNotNull(actual);
            byte[] buffer = new byte[1024];
            int count = actual.Read(buffer, 0, buffer.Length);
            Assert.IsTrue(actual.ReadByte() == -1);
            string result = Encoding.UTF8.GetString(buffer, 0, count);
            string expected = "--abcdefg\r\nContent-Type: text/plain\r\nContent-Description: 200/100\r\n\r\nHello1\r\n--abcdefg\r\nContent-Type: text/html\r\nContent-Description: 400/300\r\n\r\nHello2\r\n--abcdefg--";
            Assert.AreEqual(expected, result);                
        }

        /// <summary>
        ///A test for ParseMultiSubscriptionParameters
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Laharsub.exe")]
        public void ParseMultiSubscriptionParametersTest()
        {
            PubSubService_Accessor target = new PubSubService_Accessor(); // TODO: Initialize to an appropriate value
            NameValueCollection queryParameters = new NameValueCollection(); 
            // subs[0][topicid]=1&subs[0][from]=2&subs[1][topicId]=3&subs[1][from]=4&subs[2][topicid]=5
            queryParameters["subs[0][topicid]"] = "1";
            queryParameters["subs[0][from]"] = "2";
            queryParameters["subs[1][topicid]"] = "3";
            queryParameters["subs[1][from]"] = "4";
            queryParameters["subs[2][topicid]"] = "5";
            Dictionary<int, int> actual = target.ParseMultiSubscriptionParameters(queryParameters);
            Assert.IsNotNull(actual);
            Assert.IsTrue(actual.Count == 3);
            Assert.IsTrue(actual[1] == 2);
            Assert.IsTrue(actual[3] == 4);
            Assert.IsTrue(actual[5] == 0);
        }
    }
}
